﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using ERP_RORZE_PERMISSION.Base;
using ERP_RORZE_PERMISSION.Base.Objects;
using ERP_RORZE_PERMISSION.Forms;

namespace ERP_RORZE_PERMISSION.UserControls
{
    public partial class ucPermission : DevExpress.XtraEditors.XtraUserControl
    {
        private int index = 0;
        public int _action = 1;

        public ucPermission()
        {
            InitializeComponent();
        }

        private void ucUser_Load(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        private void btRefresh_Click(object sender, EventArgs e)
        {
            BindDataGrid();
        }
        
        private void addNewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Configure();
        }


        private void gridView1_DoubleClick(object sender, EventArgs e)
        {

        }

        private void gridView1_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
        {

            if (gridView1.RowCount != 0)
                return;

            StringFormat drawFormat = new StringFormat();

            drawFormat.Alignment = drawFormat.LineAlignment = StringAlignment.Center;

            e.Graphics.DrawString("No items exist in this view", e.Appearance.Font, SystemBrushes.ControlDark, new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), drawFormat);

        }

        private void btConfig_Click(object sender, EventArgs e)
        {
            Configure();

        }

        #region Private methods

        public void BindDataGrid()
        {
            gridControl1.DataSource = BaseDAO.ExecuteQuery("SELECT [PermissionID],[222_Permission].[FunctionID],[222_Functions].FunctionName" +
                                                           ",[UserGroupID],[222_UserGroup].GroupName,[Status] " +
                                                           "FROM [222_Permission] left join [222_Functions] on [222_Functions].FunctionID = [222_Permission].FunctionID " +
                                                           "left join [222_UserGroup] on [222_Permission].UserGroupID = [222_UserGroup].GroupID");
            gridView1.BestFitColumns();
            Utilities.GridUtil.SelectRow(gridView1, index, _action);
        }
        
        private void Configure()
        {
            Cursor.Current = Cursors.WaitCursor;
            frm_PermissionConfig config= new frm_PermissionConfig();
            config.StartPosition = FormStartPosition.CenterScreen;
            config.Show();
            Cursor.Current = Cursors.Default;
        }

        #endregion



    }
}
